package cn.itsource.org.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.EmailUtils;
import cn.itsource.basic.utils.PageResult;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @Autowired
    private EmailUtils emailUtils;

    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 修改对象
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId()==null){
                shopService.save(shop);
            }else{
                shopService.update(shop);
            }

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }
//     this.$http.get("/shop/email/"+row.id)
    @GetMapping("/email/{id}")
    public AjaxResult email(@PathVariable("id")Long id) {
        //1. 把 ID为27 店铺的状态改为激活
        Shop shop = shopService.queryById(id);

        System.out.println(shop);
        //激活
        shop.setState(1);
        shopService.update(shop);

        //2. 给ID为27 店铺 关联的管理员的 邮箱发邮件
        emailUtils.sendEmail(shop.getAdmin().getEmail(),shop.getAdmin().getId());

        return AjaxResult.me();
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping
    public List<Shop> list(){

        return shopService.query();
    }

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageResult<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }

    /**
     * 添加对象
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping("/settledIn")
    public AjaxResult settledIn(@RequestBody Shop shop){
        System.out.println(shop);
        System.out.println(shop.getAdmin());
        try {
            //服务层没有这个方法-1.保存一个employee对象  2.保存shop的能力（拿到新加employee对象的ID）
            shopService.settledIn(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
}
